package org.will.cqrs.command;

import org.reveno.atp.api.transaction.TransactionContext;
import org.will.ddd.domain.IAggregateRoot;
import org.will.ddd.eventing.IDomainEvent;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
 * Created by EdifierWill on 2016/8/11.
 */
public abstract class CommandAction implements ICommandAction{

    public final ICommand _cmd;
    public final long _id;

    public CommandAction(ICommand cmd, long id) {
        this._cmd = cmd;
        this._id = id;
    }

    public abstract void handler(ICommandAction a, TransactionContext ctx);

    public void publishEvents(IAggregateRoot model,TransactionContext ctx){
        int eventsVersion = 0;
        for(IDomainEvent event:model.GetChanges()){
            ctx.eventBus().publishEvent(event);
            eventsVersion = event.getVersion();
        }
        model.AcceptChanges(eventsVersion);
    }

}
